package cn.rwklyd.BookKeeping.controller;

import cn.rwklyd.BookKeeping.pojo.Result;
import cn.rwklyd.BookKeeping.pojo.vo.ChartDataVO;
import cn.rwklyd.BookKeeping.pojo.vo.RankingItemVO;
import cn.rwklyd.BookKeeping.service.StatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/statistics")
@Tag(name = "统计接口", description = "提供统计数据相关接口，包括图表数据和排行榜数据")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    @GetMapping("/chart")
    @Operation(
        summary = "获取图表数据", 
        description = "根据类型和时间范围获取图表数据，包括总金额、平均金额、图表数据和标签"
    )
    @ApiResponses(value = {
        @ApiResponse(
            responseCode = "200", 
            description = "成功获取图表数据",
            content = @Content(mediaType = "application/json", schema = @Schema(implementation = ChartDataVO.class))
        )
    })
    public Result<ChartDataVO> getChartData(
            @Parameter(description = "类型：expense-支出，income-收入", schema = @Schema(allowableValues = {"expense", "income"})) 
            @RequestParam(defaultValue = "expense") String type,
            
            @Parameter(description = "时间范围：week-周，month-月，year-年", schema = @Schema(allowableValues = {"week", "month", "year"})) 
            @RequestParam(defaultValue = "week") String timeRange) {
        ChartDataVO chartData = statisticsService.getChartData(type, timeRange);
        return Result.success(chartData);
    }

    @GetMapping("/ranking")
    @Operation(
        summary = "获取排行榜数据", 
        description = "根据类型和时间范围获取排行榜数据，按金额降序排序"
    )
    @ApiResponses(value = {
        @ApiResponse(
            responseCode = "200", 
            description = "成功获取排行榜数据",
            content = @Content(mediaType = "application/json", schema = @Schema(implementation = RankingItemVO.class))
        )
    })
    public Result<List<RankingItemVO>> getRankingList(
            @Parameter(description = "类型：expense-支出，income-收入", schema = @Schema(allowableValues = {"expense", "income"})) 
            @RequestParam(defaultValue = "expense") String type,
            
            @Parameter(description = "时间范围：week-周，month-月，year-年", schema = @Schema(allowableValues = {"week", "month", "year"})) 
            @RequestParam(defaultValue = "week") String timeRange) {
        List<RankingItemVO> rankingList = statisticsService.getRankingList(type, timeRange);
        return Result.success(rankingList);
    }
} 